package com.wyh.service.serviceImpl.gfiveImpl.tree_service_impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wyh.domain.vo.gfive.treeVo.BuildingUnitsVO;
import com.wyh.domain.vo.gfive.treeVo.LayerVO;
import com.wyh.domain.vo.gfive.treeVo.RoomVO;
import com.wyh.mapper.gfivemapper.tree_mapper.BuildingUnitsVOMapper;
import com.wyh.mapper.gfivemapper.tree_mapper.LayerVOMapper;
import com.wyh.mapper.gfivemapper.tree_mapper.ProjectVOMapper;
import com.wyh.mapper.gfivemapper.tree_mapper.RoomVOMapper;
import com.wyh.service.gfive.tree_service.IRoomVOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class RoomVOServiceImpl extends ServiceImpl<RoomVOMapper, RoomVO>
implements IRoomVOService {
    @Autowired
    private BuildingUnitsVOMapper buildingUnitsVOMapper;
    @Autowired
    private ProjectVOMapper projectVOMapper;
    @Autowired
    private LayerVOMapper layerVOMapper;
    @Autowired
    private RoomVOMapper mapper;
    @Override
    public List<RoomVO> findAll() {
        return mapper.selectAll();
    }
   private ArrayList<Integer> list = new ArrayList<>();
    @Override
    public List<RoomVO> findList(Integer id) {
        QueryWrapper<RoomVO> wrapper = new QueryWrapper<>();
        wrapper.eq("buildingid",id);
        List<RoomVO> roomVOS = mapper.selectList(wrapper);
        return roomVOS;
    }

    @Override
    public String getInfo(Integer id) {
         String s = mapper.selectInfo(id);
        return s;
    }

    @Override
    public void updateById(String infoname,Integer id) {
        mapper.updateByID(infoname,id);
    }

    @Override
    public HashMap<Integer, List<Integer>> getResourceIdsByProjectIds(List<Integer> projectIds) {
        HashMap<Integer, List<Integer>> resultMap = new HashMap<>();

        for (Integer projectId : projectIds) {
            // 获取对应的buildingunit，且state为1
            QueryWrapper<BuildingUnitsVO> buildingUnitQueryWrapper = new QueryWrapper<>();
            buildingUnitQueryWrapper.eq("parentid", projectId)
                    .eq("isconnected", "Y")
                    .eq("state", 1);
            List<BuildingUnitsVO> buildingUnits = buildingUnitsVOMapper.selectList(buildingUnitQueryWrapper);

            if (!buildingUnits.isEmpty()) {
                List<Integer> buildingUnitIds = getBuildingUnits(buildingUnits, new ArrayList<>());

                // 获取对应的layer，且state为1
                QueryWrapper<LayerVO> layerQueryWrapper = new QueryWrapper<>();
                layerQueryWrapper.in("parentid", buildingUnitIds)
                        .eq("state", 1);
                List<LayerVO> layers = layerVOMapper.selectList(layerQueryWrapper);

                if (!layers.isEmpty()) {
                    List<Integer> layerIds = new ArrayList<>();
                    for (LayerVO layer : layers) {
                        layerIds.add(layer.getId());
                    }

                    // 获取对应的resource，且state为1或2
                    QueryWrapper<RoomVO> resourceQueryWrapper = new QueryWrapper<>();
                    resourceQueryWrapper.in("buildingid", layerIds)
                            .in("state", Arrays.asList(1, 2));
                    List<RoomVO> resources = mapper.selectList(resourceQueryWrapper);

                    List<Integer> resourceIds = new ArrayList<>();
                    for (RoomVO resource : resources) {
                        resourceIds.add(resource.getId());
                    }

                    resultMap.put(projectId, resourceIds);
                }
            }
        }

        return resultMap;
    }

    //    在下面这里找出与layer表直接关联的id集合，主要思路是先找到与project表直接关联的id集合
//    往下去找有没有isconnected为"N"的记录挂在与project表直接关联的id的记录下
//    一直往下遍历，如果遍历不到则说明该记录他直接关联了下一张，这就是我们要的id的集合。
    /**
     * 递归获取与 project 表直接或间接关联的 buildingunit 记录的 id 集合。
     *
     * @param buildingUnitsVOS 初始的 buildingunit 记录列表
     * @param list             用于存储结果的 id 集合
     * @return 与 project 表直接或间接关联的 buildingunit 记录的 id 集合
     */
    public List<Integer> getBuildingUnits(List<BuildingUnitsVO> buildingUnitsVOS, List<Integer> list) {
        for (BuildingUnitsVO buildingUnit : buildingUnitsVOS) {
            // 查找 state 为 1 且 isconnected 为 "N" 的子记录
            QueryWrapper<BuildingUnitsVO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parentid", buildingUnit.getId())
                    .eq("state", 1)
                    .eq("isconnected", "N");
            List<BuildingUnitsVO> childBuildingUnits = buildingUnitsVOMapper.selectList(queryWrapper);

            if (childBuildingUnits.isEmpty()) {
                list.add(buildingUnit.getId());
            } else {
                getBuildingUnits(childBuildingUnits, list);
            }
        }
        return list;
    }

}
